package com.zl.easybuy.service.impl;

import com.zl.easybuy.mapper.UserMapper;
import com.zl.easybuy.pojo.User;
import com.zl.easybuy.service.UserService;
import com.zl.easybuy.util.PasswordUtils;
import com.zl.easybuy.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public int addUser(User user) {

        userMapper.addUser(user);
        return 1;
    }

    @Override
    public User queryById(Integer id) {
        User user = userMapper.queryById(id);
        return user;
    }

    @Override
    public boolean login(String loginName, String password) {
        User user=userMapper.queryByName(loginName);
        if(user!=null){
            if(PasswordUtils.verify(password,user.getPassword())){
                SessionUtil.setLoginSession(user);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean register(User user) {
        if (user!=null){
            if(userMapper.queryByName(user.getLoginName())==null){
                if(userMapper.addUser(user)>0){
                    return true;
                }
            }
        }
        return false;
    }
}
